package com.itwn.comic.web;


import com.itwn.comic.entity.Slideshow;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.SlideshowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("slideshow")
public class SlideshowController {
    @Autowired
    SlideshowService slideshowService;

    @GetMapping("/list")
    public ResponseResult<List<Slideshow>> initSlideshow() {
        ResponseResult<List<Slideshow>> result = new ResponseResult<>();
        try {
            List<Slideshow> list = this.slideshowService.queryAll();
            result.setMsg("查询轮播图成功!");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询轮播图失败!");
            result.setCode(201);
        }
        return result;
    }

    //添加
    @PostMapping("/add")
    public ResponseResult<Void> addTag(@RequestBody Slideshow slideshow) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            slideshowService.addSlideshow(slideshow);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return result;
    }

    @PutMapping("/modify")
    public ResponseResult<Void> modifySlideshow(@RequestBody Slideshow slideshow) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            slideshowService.modifySlideshow(slideshow);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }

    //删除标签
    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> delSlideshow(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            slideshowService.delSlideshow(id);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return result;
    }

    /**
     *
     *  轮播图的图片上传
     */

    @RequestMapping("/slideshowImgUpload")
    public ResponseResult<String> slideshowImgUpload(MultipartFile multipartFile){
        ResponseResult<String> result = new ResponseResult<>() ;
        try {
            String fileName = UUID.randomUUID().toString()+".jpg";
            String filePath = "D:/Imgs/slideshow/" + fileName ;
            File file = new File(filePath);
            multipartFile.transferTo(file);
            result.setCode(200);
            result.setMsg("图片上传成功");
            result.setData(fileName);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,图片上传失败");
        }

        return result ;
    }
}
